<template>
	<div class="content">
		<div class="treeContent topContent">
			<div style="margin-bottom: 15px;margin-left: 15px;"><span>用户信息</span></div>
			<div class="content-th">
				<div class="flex1">用户名称</div>
				<div class="flex1">会员级别</div>
				<div class="flex1">节点级别</div>
				<div class="flex1">注册时间</div>
			</div>
			<div class="content-tr">
				<div class="flex1" v-text="detail.userName">#</div>
				<div class="flex1" v-text="detail.levelName">#</div>
				<div class="flex1" v-text="detail.nodeName">#</div>
				<div class="flex1" v-text="detail.createTime != '#' ? stringToTime(detail.createTime) : '#'">#</div>
			</div>
		</div>
		<div class="treeContent midContent" id="midContent">
			<div class="userSearch">
				<i-input icon="ios-search-outline" placeholder="请输入用户ID" style="width: 200px" v-model="searchId" @on-click="searchUser" @on-enter="searchUser"></i-input>
			</div>
			<div id="container">
				<ul class="tree" v-show="JSON.stringify(list) != '[]'">
					<li class="tree-li">
						<a @click="show(list)" :id="'u_' + list.uuid">
							<span v-text="list.uuid"></span>
							<Icon :id="'ios_' + list.uuid" style="position: absolute;top: 100%;left: 50%;z-index: 9;background: #FFFFFF;border-radius: 100%;border: 1px solid #ccc;transform: translate(-50%,-50%);" type="ios-add" size="17" color="#666" v-show="JSON.stringify(list.children) != '[]'"></Icon>
						</a>
						<ul style="opacity: 0;position: relative;" v-show="JSON.stringify(list.children) != '[]'">
							<!--通过组件递归完成-->
							<my-tree :model="item" v-for="(item,index) of list.children" :key="index" v-if="JSON.stringify(list.children) != '[]'"></my-tree>
						</ul>
					</li>
				</ul>
			</div>
		</div>
		<div class="demo-spin-container" v-if="spinShow">
			<Spin fix></Spin>
		</div>
	</div>
</template>

<script>
	import myTree from './treeMenu'
	export default {
		name: 'treeList',
		components: {
			myTree
		},
		data() {
			return {
				data1: [],
				sum: '#',
				children: [],
				maxList: [],
				minList: [],
				user: [],
				spinShow: true,
				detail: {
					userName: '#',
					levelName: '#',
					nodeName: '#',
					createTime: '#',
				},
				searchId: '',
				columns1: [{
						title: '用户ID',
						key: 'uuid',
						align: 'center',
						render: (h, params) => {
							return h('div', params.row.uuid == null ? '#' : params.row.uuid)
						}
					},
					{
						title: '用户账号',
						align: 'center',
						key: 'nickName',
						render: (h, params) => {
							return h('div', params.row.phone == null ? params.row.email : params.row.phone)
						}
					},
					{
						title: '用户类型',
						align: 'center',
						key: 'levelId',
						render: (h, params) => {
							return h('div', params.row.typeId == null ? '#' : params.row.typeId == 1 ? '普通用户' : params.row.typeId == 2 ? '会员' : '')
						}
					},
					{
						title: '业绩',
						align: 'center',
						key: 'sum',
						render: (h, params) => {
							return h('div', params.row.sum == null ? '#' : params.row.sum + ' M')
						}
					},
					{
						title: '注册时间',
						align: 'center',
						key: 'createTime',
						render: (h, params) => {
							return h('div', params.row.createTime == null ? '#' : params.row.createTime)
						}
					},
				],
				list: [],
			}
		},
		mounted() {
			let token = localStorage.getItem('mytoken')
			this.axios.get('/admin/organization/relationship').then(res => {
				if(res.data.code == 200) {
					this.list = res.data.data;
					this.user.push(this.list);
					this.spinShow = false;
				}
			})
			//监听localstorage key为userDetail 具体方法在mian.js
			this.$nextTick(() => {
				window.addEventListener('setItem', () => {
					this.detail = JSON.parse(localStorage.userDetail);
				})
			})
		},
		methods: {
			show(list) {
				//重置
				for(let i = 0; i < document.getElementsByTagName('a').length; i++) {
					document.getElementsByTagName('a')[i].style.color = "#666";
					document.getElementsByTagName('a')[i].style.background = "#FFFFFF";
				}
				document.getElementById('u_' + list.uuid).style.color = '#FFFFFF';
				document.getElementById('u_' + list.uuid).style.background = '#2D8cF0';
				if(document.getElementById('u_' + list.uuid).nextSibling.style.opacity == '1') {
					setTimeout(() => {
						document.getElementById('u_' + list.uuid).nextSibling.style.display = 'none';
					}, 0)
					document.getElementById('u_' + list.uuid).nextSibling.style.opacity = '0';
				} else {
					document.getElementById('u_' + list.uuid).nextSibling.style.opacity = '1';
					document.getElementById('u_' + list.uuid).nextSibling.style.display = 'block';
				}
				//修改按钮样式
				if(document.getElementById('ios_' + list.uuid)) {
					let updateClass = document.getElementById('ios_' + list.uuid);
					if(updateClass.className.indexOf('ivu-icon-ios-add') != -1) {
						updateClass.className = updateClass.className.replace('ivu-icon-ios-add', 'ivu-icon-ios-remove');
					} else {
						updateClass.className = updateClass.className.replace('ivu-icon-ios-remove', 'ivu-icon-ios-add');
					}
				}
				//自己修改的存储，用于监听
				this.resetSetItem('userDetail', JSON.stringify(list));
			},
			//搜索
			searchUser() {
				//判断是否有这个用户
				if(!document.getElementById('u_' + this.searchId)) {
					return false;
				}
				//移动到可以看到dom元素的窗口距离
				window.location.hash = '#u_' + this.searchId;
				//触发dom元素的点击事件
				document.getElementById('u_' + this.searchId).click();
				//显示该dom元素所属上级元素
				//循环判断ul的className是否为tree，为tree代表最高一级，则跳出循环
				let isFirst = true;
				let firstNode = document.getElementById('u_' + this.searchId).parentNode.parentNode;
				while(isFirst) {
					if(firstNode.className != 'tree') {
						setTimeout(() => {
							firstNode.style.display = 'block';
						}, 0)
						firstNode.style.opacity = '1';
						firstNode.style.position = 'relative';
						//修改按钮样式
						let updateBtnClass = firstNode.previousElementSibling.children[1];
						if(updateBtnClass.className.indexOf('ivu-icon-ios-add') != -1) {
							updateBtnClass.className = updateBtnClass.className.replace('ivu-icon-ios-add', 'ivu-icon-ios-remove');
						} else {
							updateBtnClass.className = updateBtnClass.className.replace('ivu-icon-ios-remove', 'ivu-icon-ios-add');
						}
						firstNode = firstNode.parentNode.parentNode;
					} else {
						isFirst = false;
					}
				}
			},
		}
	}
</script>

<style lang="less" scoped="scoped">
	/deep/ .ivu-tree-title {
		border: 1px solid #CCCCCC;
		padding: 5px 20px;
		background: #FFFFFF;
	}
	
	/deep/ .ivu-tree-title:hover {
		box-shadow: 0 1px 6px rgba(0, 0, 0, 0.2);
	}
	
	/deep/ .ivu-icon-md-arrow-dropright:before {
		position: relative;
		top: 5px;
	}
	
	.content {
		background: #f5f7f9;
		border: 0;
		padding: 0;
		position: relative;
		.treeContent {
			background: #FFFFFF;
			border: 1px solid #CCCCCC;
			padding: 15px;
			margin-bottom: 20px;
			border-radius: 4px;
		}
	}
	
	.content-th {
		display: flex;
		text-align: center;
		height: 40px;
		line-height: 40px;
		color: #515a6e;
		font-weight: bold;
		background: #f5f7f9;
		border: 1px solid rgb(229, 229, 229);
	}
	
	.content-tr {
		display: flex;
		text-align: center;
		height: 40px;
		line-height: 40px;
		color: #515a6e;
		border: 1px solid rgb(229, 229, 229);
		border-top: 0;
	}
	
	.flex1 {
		flex: 1;
	}
	
	.midContent {
		height: calc(~'100vh - 310px');
		margin-bottom: 0 !important;
		overflow-x: auto;
	}
	
	.userSearch {
		position: fixed;
		right: 33px;
		z-index: 1;
	}
	
	/deep/ .ivu-tree-title-selected {
		background: #2d8cf0 !important;
		color: #fff;
	}
	
	#container {
		margin: 0 auto;
	}
	
	.tree {
		width: 100%;
		padding-top: 40px !important;
	}
	
	.tree-li {
		min-width: 100%;
		ul{
			display: none;
		}
	}
	
	.tree span {
		display: block;
		font-size: .9rem;
		/*margin-top: .5rem;*/
	}
	
	.tree img {
		display: block;
		margin: 0 auto;
		margin-bottom: 1rem;
		width: 150px;
	}
	
	.tree,
	.tree ul {
		padding-top: 20px;
		position: relative;
		-webkit-transition: all 0.5s;
		transition: all 0.5s;
		white-space: nowrap;
	}
	
	.tree li {
		/*float: left;*/
		display: inline-block;
		vertical-align: top;
		text-align: center;
		list-style-type: none;
		position: relative;
		padding: 20px 5px 0 5px;
		-webkit-transition: all 0.5s;
		transition: all 0.5s;
	}
	
	.tree li::before,
	.tree li::after {
		content: '';
		position: absolute;
		top: 0;
		right: 50%;
		border-top: 1px solid #ccc;
		width: 50%;
		height: 20px;
	}
	
	.tree li::after {
		right: auto;
		left: 50%;
		border-left: 1px solid #ccc;
	}
	
	.tree li:only-child::after,
	.tree li:only-child::before {
		display: none;
	}
	
	.tree li:only-child {
		padding-top: 0;
	}
	
	.tree li:first-child::before,
	.tree li:last-child::after {
		border: 0;
	}
	
	.tree li:last-child::before {
		border-right: 1px solid #ccc;
		border-radius: 0 5px 0 0;
	}
	
	.tree li:first-child::after {
		border-radius: 5px 0 0 0;
	}
	
	.tree ul::before {
		content: '';
		position: absolute;
		top: 0;
		left: 50%;
		border-left: 1px solid #ccc;
		width: 0;
		height: 20px;
	}
	
	.tree li a {
		border: 1px solid #ccc;
		padding: 10px;
		text-decoration: none;
		width: -webkit-min-content;
		width: -moz-min-content;
		width: min-content;
		color: #666;
		font-size: 1rem;
		display: inline-block;
		border-radius: 5px;
		-webkit-transition: all 0.5s;
		transition: all 0.5s;
		position: relative;
	}
	
	.tree li a {
		background: #FFFFFF;
	}
	
	.tree li a:hover,
	.tree li a:hover+ul li a {
		border: 1px solid #2D8cF0;
	}
	
	.tree li a:hover+ul li::after,
	.tree li a:hover+ul li::before,
	.tree li a:hover+ul::before,
	.tree li a:hover+ul ul::before {
		/*border-color: #2D8cF0;*/
	}
</style>